package na.edu.polytechnic;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.maps.ItemizedOverlay;

public class ItemizedOverlayArray extends ItemizedOverlay<mOverlayItem> {
   protected HashMap<String, Drawable> icons = new HashMap<String, Drawable>();
   protected ArrayList<mOverlayItem> currentlyVisible = new ArrayList<mOverlayItem>(); //Currently visible items
   
   protected ArrayList<mOverlayItem> all = new ArrayList<mOverlayItem>(); //All the items   
   //Categories:
   protected ArrayList<mOverlayItem> attraction = new ArrayList<mOverlayItem>();
   protected ArrayList<mOverlayItem> sport = new ArrayList<mOverlayItem>();
   protected ArrayList<mOverlayItem> shopping = new ArrayList<mOverlayItem>();
   protected ArrayList<mOverlayItem> restaurants = new ArrayList<mOverlayItem>();
   protected ArrayList<mOverlayItem> entertainment = new ArrayList<mOverlayItem>();
   protected ArrayList<mOverlayItem> transportation = new ArrayList<mOverlayItem>();
   Context mContext;

   public ItemizedOverlayArray(final Drawable defaultMarker, final Context context) {
      super(boundCenterBottom(defaultMarker));
      mContext = context;
      currentlyVisible = new ArrayList<mOverlayItem>();
   }

   public void addToCategory(final mOverlayItem overlay) {    
      if (!icons.containsKey(overlay.type)) {
         Resources res = mContext.getResources();
         final Drawable d = res.getDrawable(res.getIdentifier("drawable/"+overlay.type, null, mContext.getPackageName()));
         //custom marker
         bindIcon(d);
         icons.put(overlay.type, d);
      }
      overlay.setMarker(icons.get(overlay.type));

      all.add(overlay);         
      //Categorize:
      if(overlay.type.equalsIgnoreCase("attraction"))
         attraction.add(overlay);
      else if(overlay.type.equalsIgnoreCase("sport"))
         sport.add(overlay);
      else if(overlay.type.equalsIgnoreCase("entertainment"))
         entertainment.add(overlay);
      else if(overlay.type.equalsIgnoreCase("restaurant"))
         restaurants.add(overlay);
      else if(overlay.type.equalsIgnoreCase("transport"))
         transportation.add(overlay);
      else if(overlay.type.equalsIgnoreCase("shopping"))
         shopping.add(overlay);     
   }
   
   public void addCategory(final ArrayList<mOverlayItem> cat) {
      currentlyVisible.addAll(cat);
      populate();
   }
   
   public void addOverlay(final mOverlayItem item) {
      currentlyVisible.add(item);
      populate();
   }

   @Override
   protected mOverlayItem createItem(final int i) {
      return currentlyVisible.get(i);
   }

   @Override
   public int size() {
      return currentlyVisible.size();
   }

   public void bindIcon(final Drawable d){
      super.boundCenterBottom(d);
   }

   //Our current location and destination...
   @Override
   protected boolean onTap(final int index) {
      final mOverlayItem item = currentlyVisible.get(index);
      //If there is no minigame option:
      if (item.minigametype == null || item.minigametype.equals("")) {
         //Then launch the info-screen automatically:
         final Intent intent = new Intent(mContext, LocationInfo.class);
         final Bundle b = new Bundle();
         b.putParcelable("item", currentlyVisible.get(index));
         intent.putExtras(b);
         mContext.startActivity(intent);
      }
      else { //If there is a minigame, then show options:
         final CharSequence[] items = {"Info","Extra stuff"};
         final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
         builder.setTitle(item.getTitle());

         builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(final DialogInterface dialog, final int i) {
               if(i == 0){
                  final Intent intent = new Intent(mContext, LocationInfo.class);
                  final Bundle b = new Bundle();
                  b.putParcelable("item", currentlyVisible.get(index));
                  intent.putExtras(b);
                  mContext.startActivity(intent);
               }
               else{
                  if (item.minigametype.equals("spot")) {
                     final Intent intent = new Intent(mContext, SpotActivity.class);
                     final Bundle b = new Bundle();
                     b.putString("xml", item.minigamedata);
                     intent.putExtras(b);
                     mContext.startActivity(intent);
                  }
                  else if (item.minigametype.equals("puzzle")) {
                     final Intent intent = new Intent(mContext, Puzzle.class);
                     final Bundle b = new Bundle();
                     b.putString("photo", item.minigamedata);
                     intent.putExtras(b);
                     mContext.startActivity(intent);
                  }
                  else if (item.minigametype.equals("tipcalc")) {
                     final Intent intent = new Intent(mContext, TipCalculator.class);
                     mContext.startActivity(intent);
                  }
               }
            }
         });

         final AlertDialog alert = builder.create();
         alert.show();
      }
      return true;
   }
}